#include "ElButton.h"
ElButton::ElButton(const QString &text, BtnType type, BtnShape shape, QWidget *parent)
        : QPushButton(text, parent), m_type(type), m_shape(shape), m_iconSize(24)
{
    updateStyle();
}

void ElButton::setBtnType(BtnType type)
{
    if (m_type != type) {
        m_type = type;
        updateStyle();
    }
}
ElButton::BtnType ElButton::btnType() const
{
    return m_type;
}
void ElButton::setBtnShape(BtnShape shape)
{
    if (m_shape != shape) {
        m_shape = shape;
        updateStyle();
    }
}
ElButton::BtnShape ElButton::btnShape() const
{
    return m_shape;
}
void ElButton::setBtnIcon(const QIcon &icon, int size)
{
    m_icon = icon;
    m_iconSize = size;
    this->setIcon(icon);
    this->setIconSize(QSize(size, size));
    updateStyle();
}
void ElButton::updateStyle()
{
    QString style;
    QString radius = "4px"; // 默认矩形圆角
    if (m_shape == Round) radius = "20px";
    if (m_shape == Circle) radius = QString::number(m_iconSize/2) + "px";

    QString padding = "6px 15px";
    if (m_shape == Circle) padding = "0px";

    switch (m_type) {
        case Default:
            style = QString(
                    "QPushButton {background:#f5f5f5; color:#606266; border-radius:%1; padding:%2;}"
                    "QPushButton:hover {background:#e6e6e6;}"
                    "QPushButton:pressed {background:#dcdcdc;}"
            ).arg(radius, padding);
            break;
        case Primary:
            style = QString(
                    "QPushButton {background:#409EFF; color:white; border-radius:%1; padding:%2;}"
                    "QPushButton:hover {background:#66b1ff;}"
                    "QPushButton:pressed {background:#3a8ee6;}"
            ).arg(radius, padding);
            break;
        case Success:
            style = QString(
                    "QPushButton {background:#67C23A; color:white; border-radius:%1; padding:%2;}"
                    "QPushButton:hover {background:#85ce61;}"
                    "QPushButton:pressed {background:#5daf34;}"
            ).arg(radius, padding);
            break;
        case Danger:
            style = QString(
                    "QPushButton {background:#F56C6C; color:white; border-radius:%1; padding:%2;}"
                    "QPushButton:hover {background:#f78989;}"
                    "QPushButton:pressed {background:#dd6161;}"
            ).arg(radius, padding);
            break;
        case Warning:
            style = QString(
                    "QPushButton {background:#E6A23C; color:white; border-radius:%1; padding:%2;}"
                    "QPushButton:hover {background:#ebb563;}"
                    "QPushButton:pressed {background:#d5942f;}"
            ).arg(radius, padding);
            break;
    }
    this->setStyleSheet(style);
}
